package dev.alm.dashscopedemo.controller;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("chat")
public class ChatController {

    private final ChatModel dashscopeChatModel;

    public ChatController(ChatModel model) {
        this.dashscopeChatModel = model;
    }

    @GetMapping("simple")
    public String simple() {
        String content = dashscopeChatModel.call(new Prompt("你好啊，你是谁？")).getResult().getOutput().getContent();
        return content;
    }

    @GetMapping("stream")
    public Flux<String> stream(HttpServletResponse response) {
        // 避免返回乱码
        response.setCharacterEncoding("UTF-8");
        Flux<ChatResponse> stream = dashscopeChatModel.stream(new Prompt("你好啊，你是谁？"));
        return stream.map(chatResponse -> chatResponse.getResult().getOutput().getContent());
    }

}
